package com.antis.olsl.magic.report.quarterly.detail;

import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.antis.olsl.arouterlib.MagicRoutePath;
import com.antis.olsl.library.MyApplication;
import com.antis.olsl.base.BaseActivity;
import com.antis.olsl.library.dialog.ChooseSellerDialog;
import com.antis.olsl.library.response.GetEmployeeInfoListResp;
import com.antis.olsl.base.utils.ToastUtil;
import com.antis.olsl.magic.databinding.ActivityQuarterlyDetailBinding;
import com.antis.olsl.magic.response.GetSalesSummaryResp;
import com.trello.rxlifecycle2.LifecycleTransformer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import timber.log.Timber;

/**
 * 季报详情
 *
 * @author hhh
 */
@Route(path = MagicRoutePath.ACTIVITY_QUARTERLY_DETAIL)
public class QuarterlyDetailActivity extends BaseActivity<ActivityQuarterlyDetailBinding> implements QuarterlyDetailContract.View, ChooseSellerDialog.ChooseSellerListener {

    @Autowired
    String salesRoomId;
    @Autowired
    int searchDate;
    @Autowired
    int searchQuarter;
    @Autowired
    String salesRoomName;

    private String mEmpCode;
    private QuarterlyDetailPresenter mPresenter;

    private ChooseSellerDialog mDialog;
    private ArrayList<GetEmployeeInfoListResp.ContentBean> sellerList = new ArrayList<>();
    private ArrayList<GetEmployeeInfoListResp.ContentBean> chooseSellerList = new ArrayList<>();
    private boolean chooseAllSalesroom;

    @Override
    protected ActivityQuarterlyDetailBinding getViewBinding() {
        return ActivityQuarterlyDetailBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @Override
    protected void initPresenter() {
        super.initPresenter();
        mPresenter = new QuarterlyDetailPresenter();
        mPresenter.takeView(this);
    }

    @Override
    protected void initViews() {
        viewBinding.rlChooseSeller.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<String, Object> map = new HashMap<>(2);
                String merchantId = null != MyApplication.getUserInfo() && !TextUtils.isEmpty(MyApplication.getUserInfo().getMerchantId()) ? MyApplication.getUserInfo().getMerchantId() : "";
                map.put("shopId", salesRoomId);
                map.put("merchantId", merchantId);
                mPresenter.getEmployeeInfoList(map);
            }
        });
    }

    @Override
    protected void initData() throws IllegalAccessException {
        String storeName;
        if (TextUtils.isEmpty(salesRoomName)) {
            storeName = "全部门店";
        } else {
            storeName = salesRoomName;
        }
        viewBinding.textStoreName.setText(storeName);
        String showDate = searchDate + "年" + searchQuarter + "季度";
        viewBinding.textDate.setText(showDate);

        loadData();
    }

    private void loadData() {
        Map<String, Object> map = new HashMap<>();
        map.put("shopId", salesRoomId);
        map.put("searchTime", searchDate);
        map.put("quarter", searchQuarter);
        map.put("empCode", mEmpCode);
        mPresenter.getSeasonSalesDetails(map);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != mPresenter) {
            mPresenter.unSubscribe();
        }
        if (null != mDialog) {
            mDialog.dismiss();
        }
    }

    @Override
    public void getSeasonSalesDetailsSuccess(GetSalesSummaryResp.ContentBean bean) {
        if (null != bean) {
            float quarterlyProgress = 0f;
            if (!TextUtils.isEmpty(bean.jmbdcl)) {
                try {
                    quarterlyProgress = Float.parseFloat(bean.jmbdcl);
                } catch (NumberFormatException e) {
                    Timber.tag("hhh").e("exception= " + e);
                }
            }
            viewBinding.quarterlySeekBar.setProgress(quarterlyProgress);
            float yearProgress = 0f;
            if (!TextUtils.isEmpty(bean.nmbdcl)) {
                try {
                    yearProgress = Float.parseFloat(bean.nmbdcl);
                } catch (NumberFormatException e) {
                    Timber.tag("hhh").e("exception= " + e);
                }
            }
            viewBinding.yearSeekBar.setProgress(yearProgress);

            setSalesAmountValue(bean);
            setSalesVolumeValue(bean);
        }
    }

    private void setSalesAmountValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.xsje)) {
            viewBinding.layoutSalesAmount.textSalesAmount.setText(bean.xsje);
        } else {
            viewBinding.layoutSalesAmount.textSalesAmount.setText("0");
        }

        if (!TextUtils.isEmpty(bean.xsjezb)) {
            String salesProportion = bean.xsjezb + "%";
            viewBinding.layoutSalesAmount.textSalesProportion.setText(salesProportion);
        } else {
            viewBinding.layoutSalesAmount.textSalesProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.hbxsje)) {
            String salesMom = bean.hbxsje + "%";
            viewBinding.layoutSalesAmount.textSalesMom.setText(salesMom);
        } else {
            viewBinding.layoutSalesAmount.textSalesMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbxsje)) {
            String salesYoy = bean.tbxsje + "%";
            viewBinding.layoutSalesAmount.textSalesYoy.setText(salesYoy);
        } else {
            viewBinding.layoutSalesAmount.textSalesYoy.setText("-");
        }
    }

    private void setSalesVolumeValue(GetSalesSummaryResp.ContentBean bean) {
        if (!TextUtils.isEmpty(bean.xssl)) {
            viewBinding.layoutSalesVolume.textSalesVolume.setText(bean.xssl);
        } else {
            viewBinding.layoutSalesVolume.textSalesVolume.setText("0");
        }

        if (!TextUtils.isEmpty(bean.xsslzb)) {
            String volumeProportion = bean.xsslzb + "%";
            viewBinding.layoutSalesVolume.textVolumeProportion.setText(volumeProportion);
        } else {
            viewBinding.layoutSalesVolume.textVolumeProportion.setText("-");
        }

        if (!TextUtils.isEmpty(bean.hb_xssl)) {
            String volumeMom = bean.hb_xssl + "%";
            viewBinding.layoutSalesVolume.textVolumeMom.setText(volumeMom);
        } else {
            viewBinding.layoutSalesVolume.textVolumeMom.setText("-");
        }

        if (!TextUtils.isEmpty(bean.tbxssl)) {
            String volumeYoy = bean.tbxssl + "%";
            viewBinding.layoutSalesVolume.textVolumeYoy.setText(volumeYoy);
        } else {
            viewBinding.layoutSalesVolume.textVolumeYoy.setText("-");
        }
    }

    @Override
    public void getSeasonSalesDetailsFailure(String msg) {
        Timber.tag("hhh").e("getSeasonSalesDetailsFailure msg= " + msg);
        if (TextUtils.isEmpty(msg)) {
            msg = "请求失败,请稍后重试";
        }
        ToastUtil.showToast(this, msg);
        setDefaultValue();
    }

    private void setDefaultValue() {
        viewBinding.quarterlySeekBar.setProgress(0);
        viewBinding.yearSeekBar.setProgress(0);
        viewBinding.layoutSalesAmount.textSalesAmount.setText("0");
        viewBinding.layoutSalesAmount.textSalesProportion.setText("-");
        viewBinding.layoutSalesAmount.textSalesMom.setText("-");
        viewBinding.layoutSalesAmount.textSalesYoy.setText("-");
        viewBinding.layoutSalesVolume.textSalesVolume.setText("0");
        viewBinding.layoutSalesVolume.textVolumeProportion.setText("-");
        viewBinding.layoutSalesVolume.textVolumeMom.setText("-");
        viewBinding.layoutSalesVolume.textVolumeYoy.setText("-");
    }

    @Override
    public void getSeasonSalesDetailsEmpty() {
        Timber.tag("hhh").e("getSeasonSalesDetailsEmpty");
        setDefaultValue();
    }

    @Override
    public void getEmployeeInfoListSuccess(List<GetEmployeeInfoListResp.ContentBean> list) {
        sellerList.clear();
        sellerList.addAll(list);

        Timber.tag("hhh").e("getEmployeeInfoListSuccess");
        mDialog = new ChooseSellerDialog(this, sellerList, QuarterlyDetailActivity.this);
    }

    @Override
    public void getEmployeeInfoListFailure(String msg) {
        Timber.tag("hhh").e("getEmployeeInfoListFailure");
        if (TextUtils.isEmpty(msg)) {
            msg = "请求失败,请稍后重试";
        }
        ToastUtil.showToast(this, msg);
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return this.bindToLifecycle();
    }

    @Override
    public void onChooseSellerListener(boolean isChooseAll, ArrayList<GetEmployeeInfoListResp.ContentBean> choosedFiledInfos, ArrayList<GetEmployeeInfoListResp.ContentBean> sourceInofs, int selectedIndex) {
        chooseAllSalesroom = isChooseAll;
        chooseSellerList = choosedFiledInfos;
        sellerList = sourceInofs;
        if (selectedIndex == -1) {
            salesRoomId = "";
            viewBinding.textSeller.setText("全部销售员");
        } else {
            salesRoomId = filterIds();
        }

        loadData();
    }

    public String filterIds() {
        ArrayList<GetEmployeeInfoListResp.ContentBean> infos = (chooseAllSalesroom ? sellerList : chooseSellerList);
        String name = "";
        String ids = "";
        for (int i = 0; i < infos.size(); i++) {
            ids += TextUtils.isEmpty(ids) ? infos.get(i).id : "," + infos.get(i).id;
            if (chooseAllSalesroom) {
                continue;
            }
            name += TextUtils.isEmpty(name) ? infos.get(i).empName : "," + infos.get(i).empName;
        }
        boolean chooseALL = false;
        if (chooseAllSalesroom) {
            name = "全部门店";
            chooseALL = true;
        }

        if (null != infos.get(0)) {
            mEmpCode = infos.get(0).empCode;
            viewBinding.textSeller.setText(infos.get(0).empName);
        }
        return ids;
    }
}
